package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.wemedia.dtos.WmMaterialDto;
import com.heima.model.common.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Autowired
    private WmMaterialService wmMaterialService;

    /**
     * 上传图片
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        WmMaterial wmMaterial = wmMaterialService.uploadPicture(multipartFile);
        if (wmMaterial == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表
     *
     * @param wmMaterialDto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmMaterialDto wmMaterialDto) {
        IPage page = wmMaterialService.findList(wmMaterialDto);
        ResponseResult responseResult = new PageResponseResult(
                wmMaterialDto.getPage(),
                wmMaterialDto.getSize(),
                (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @GetMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable Long id) {
        return wmMaterialService.collect(id, 1);
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable Long id) {
        return wmMaterialService.collect(id, 0);
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @GetMapping("/del_picture/{id}")
    public ResponseResult delete(@PathVariable Long id) {
        return wmMaterialService.delete(id);
    }
}